#include <QApplication>
#include "ElaApplication.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QCheckBox>
#include <QGroupBox>
#include <QSlider>
#include <QSpinBox>

// ElaWidget 相关定义
#include "ElaWidget.h"
#include "ElaScrollArea.h"

class ExampleElaScrollArea : public ElaWidget
{
    Q_OBJECT

public:
    explicit ExampleElaScrollArea(QWidget *parent = nullptr);
    ~ExampleElaScrollArea();

private:
    void setupUI();
    void setupScrollContent();

private:
    ElaScrollArea *m_scrollArea;
    QWidget *m_scrollContent;
};

ExampleElaScrollArea::ExampleElaScrollArea(QWidget *parent) 
    : ElaWidget(parent), m_scrollArea(nullptr), m_scrollContent(nullptr)
{
    setWindowButtonFlags(ElaAppBarType::StayTopButtonHint | ElaAppBarType::MinimizeButtonHint | ElaAppBarType::MaximizeButtonHint | ElaAppBarType::CloseButtonHint | ElaAppBarType::ThemeChangeButtonHint | ElaAppBarType::NavigationButtonHint);
    setupUI();
}

ExampleElaScrollArea::~ExampleElaScrollArea()
{
}

void ExampleElaScrollArea::setupUI()
{
    // 创建主布局
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(10, 10, 10, 10);
    mainLayout->setSpacing(10);

    // 创建控制面板
    QGroupBox *controlGroup = new QGroupBox("滚动区域控制", this);
    QHBoxLayout *controlLayout = new QHBoxLayout(controlGroup);

    // 手势控制复选框
    QCheckBox *gestureCheckBox = new QCheckBox("启用手势滚动", controlGroup);
    gestureCheckBox->setChecked(true);

    // 越界滚动控制
    QCheckBox *hOvershootCheckBox = new QCheckBox("水平越界滚动", controlGroup);
    hOvershootCheckBox->setChecked(true);

    QCheckBox *vOvershootCheckBox = new QCheckBox("垂直越界滚动", controlGroup);
    vOvershootCheckBox->setChecked(true);

    // 动画控制
    QCheckBox *hAnimationCheckBox = new QCheckBox("水平滚动动画", controlGroup);
    hAnimationCheckBox->setChecked(true);

    QCheckBox *vAnimationCheckBox = new QCheckBox("垂直滚动动画", controlGroup);
    vAnimationCheckBox->setChecked(true);

    controlLayout->addWidget(gestureCheckBox);
    controlLayout->addWidget(hOvershootCheckBox);
    controlLayout->addWidget(vOvershootCheckBox);
    controlLayout->addWidget(hAnimationCheckBox);
    controlLayout->addWidget(vAnimationCheckBox);
    controlLayout->addStretch();

    // 创建滚动区域
    m_scrollArea = new ElaScrollArea(this);
    m_scrollArea->setFrameShape(QFrame::NoFrame);
    m_scrollArea->setWidgetResizable(true);

    // 设置手势滚动
    m_scrollArea->setIsGrabGesture(true, 0.3);

    // 设置越界滚动
    m_scrollArea->setIsOverShoot(Qt::Horizontal, true);
    m_scrollArea->setIsOverShoot(Qt::Vertical, true);

    // 设置滚动动画
    m_scrollArea->setIsAnimation(Qt::Horizontal, true);
    m_scrollArea->setIsAnimation(Qt::Vertical, true);

    // 创建滚动内容
    setupScrollContent();

    // 添加到主布局
    mainLayout->addWidget(controlGroup);
    mainLayout->addWidget(m_scrollArea);

    // 连接信号槽
    connect(gestureCheckBox, &QCheckBox::toggled, this, [this](bool checked) { m_scrollArea->setIsGrabGesture(checked, 0.3); });

    connect(hOvershootCheckBox, &QCheckBox::toggled, this, [this](bool checked) { m_scrollArea->setIsOverShoot(Qt::Horizontal, checked); });

    connect(vOvershootCheckBox, &QCheckBox::toggled, this, [this](bool checked) { m_scrollArea->setIsOverShoot(Qt::Vertical, checked); });

    connect(hAnimationCheckBox, &QCheckBox::toggled, this, [this](bool checked) { m_scrollArea->setIsAnimation(Qt::Horizontal, checked); });

    connect(vAnimationCheckBox, &QCheckBox::toggled, this, [this](bool checked) { m_scrollArea->setIsAnimation(Qt::Vertical, checked); });
}

void ExampleElaScrollArea::setupScrollContent()
{
    // 创建滚动内容部件
    m_scrollContent = new QWidget();
    QVBoxLayout *contentLayout = new QVBoxLayout(m_scrollContent);
    contentLayout->setSpacing(15);
    contentLayout->setContentsMargins(20, 20, 20, 20);

    // 添加标题
    QLabel *titleLabel = new QLabel("ElaScrollArea 示例内容", m_scrollContent);
    titleLabel->setStyleSheet("QLabel { font-size: 24px; font-weight: bold; color: #2c3e50; }");
    contentLayout->addWidget(titleLabel);

    // 添加描述文本
    QLabel *descLabel = new QLabel(
        "这是一个展示 ElaScrollArea 功能的示例。ElaScrollArea 提供了以下特性：\n"
        "• 自定义滚动条样式\n"
        "• 手势滚动支持\n"
        "• 越界滚动效果\n"
        "• 平滑滚动动画\n"
        "• 高性能的60FPS滚动\n"
        "• 可配置的滚动参数",
        m_scrollContent);
    descLabel->setStyleSheet("QLabel { font-size: 14px; color: #34495e; background-color: #ecf0f1; padding: 10px; border-radius: 5px; }");
    descLabel->setWordWrap(true);
    contentLayout->addWidget(descLabel);

    // 添加多个示例项目
    for (int i = 1; i <= 20; ++i)
    {
        QGroupBox *itemGroup = new QGroupBox(QString("示例项目 %1").arg(i), m_scrollContent);
        QVBoxLayout *itemLayout = new QVBoxLayout(itemGroup);

        // 添加一些控件
        QLabel *itemLabel = new QLabel(QString("这是第 %1 个示例项目的内容区域。").arg(i), itemGroup);
        itemLabel->setWordWrap(true);

        QPushButton *itemButton = new QPushButton(QString("按钮 %1").arg(i), itemGroup);
        itemButton->setStyleSheet(
            "QPushButton { padding: 8px; background-color: #3498db; color: white; border: none; border-radius: 4px; }"
            "QPushButton:hover { background-color: #2980b9; }");

        QSlider *itemSlider = new QSlider(Qt::Horizontal, itemGroup);
        itemSlider->setRange(0, 100);
        itemSlider->setValue(i * 5);

        itemLayout->addWidget(itemLabel);
        itemLayout->addWidget(itemButton);
        itemLayout->addWidget(itemSlider);

        contentLayout->addWidget(itemGroup);
    }

    // 添加底部空间
    contentLayout->addStretch();

    // 设置滚动区域的内容
    m_scrollArea->setWidget(m_scrollContent);
}

// 主函数
int main(int argc, char *argv[])
{
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
    QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
    QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
#else
    qputenv("QT_SCALE_FACTOR", "1.5");
#endif
#endif

    QApplication app(argc, argv);
    eApp->init();

    // 设置应用程序样式
    app.setStyle("Fusion");

    // 创建示例窗口
    ExampleElaScrollArea window;
    window.setWindowTitle("ElaScrollArea 完整示例 (基于 ElaWidget)");
    window.resize(800, 600);
    window.moveToCenter(); // 使用 ElaWidget 的居中功能
    window.show();

    return app.exec();
}

#include "ElaScrollAreaTest.moc"